package com.baox.agridata.synchronous.service.business.impl;

import com.alibaba.fastjson.JSONObject;
import com.baox.agridata.synchronous.constant.Constant;
import com.baox.agridata.synchronous.entity.quanguo.Jyzscpb;
import com.baox.agridata.synchronous.entity.quanguo.Jyzsdwb;
import com.baox.agridata.synchronous.entity.shanxi.TCCAAEntity;
import com.baox.agridata.synchronous.entity.shanxi.TCCABEntity;
import com.baox.agridata.synchronous.entity.shanxi.TCCPAEntity;
import com.baox.agridata.synchronous.entity.shanxi.TCCPBEntity;
import com.baox.agridata.synchronous.schedule.SplitDataSummary;
import com.baox.agridata.synchronous.service.business.SupplementDataService;
import com.baox.agridata.synchronous.service.shanxi.TCCAAService;
import com.baox.agridata.synchronous.service.shanxi.TCCABService;
import com.baox.agridata.synchronous.service.shanxi.TCCPAService;
import com.baox.agridata.synchronous.service.shanxi.TCCPBService;
import com.baox.agridata.synchronous.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SupplementDataServiceImpl implements SupplementDataService {


    @Autowired
    private TCCAAService tccaaService;


    @Autowired
    private TCCABService tccabService;


    @Autowired
    private TCCPAService tccpaService;


    @Autowired
    private TCCPBService tccpbService;

    @Autowired
    private SplitDataSummary splitDataSummary;


    public static Integer PRODUCT_A = 1;
    public static Integer PRODUCT_B = 2;
    public static Integer ANIMAL_A = 1;
    public static Integer ANIMAL_B = 2;


    @Override
    public Map supplementAnimalA(String factoryCode) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 100);
        resultMap.put("msg", "检疫证号:" + factoryCode + "未查询到！");
        if (StringUtils.isEmpty(factoryCode)) {
            resultMap.put("msg", "检疫证号不能为空！");
            return resultMap;
        }
        List<Jyzsdwb> jyzsdwbs = tccaaService.getLocalCCAAList(null, null, factoryCode);
        if (jyzsdwbs != null && jyzsdwbs.size() > 0) {
            Integer result = splitDataSummary.interceptListAnimal(jyzsdwbs, ANIMAL_A);
            if (result != null && result.intValue() > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "补充了AnimalA:" + factoryCode);
                return resultMap;
            } else {
                resultMap.put("code", 100);
                resultMap.put("msg", "检疫证号：" + factoryCode + "查询到了，推送农业部失败了！");
                return resultMap;
            }
        } else {
            return resultMap;
        }
    }


    @Override
    public Map supplementAnimalB(String factoryCode) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 100);
        resultMap.put("msg", "检疫证号:" + factoryCode + "未查询到！");
        if (StringUtils.isEmpty(factoryCode)) {
            resultMap.put("msg", "检疫证号不能为空！");
            return resultMap;
        }
        List<Jyzsdwb> jyzsdwbs = tccabService.getLocalCCABList(null, null, factoryCode);
        if (jyzsdwbs != null && jyzsdwbs.size() > 0) {
            Integer result = splitDataSummary.interceptListAnimal(jyzsdwbs, ANIMAL_B);
            if (result != null && result.intValue() > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "补充了AnimalB:" + factoryCode);
                return resultMap;
            } else {
                resultMap.put("code", 100);
                resultMap.put("msg", "检疫证号：" + factoryCode + "查询到了，推送农业部失败了！");
                return resultMap;
            }
        } else {
            return resultMap;
        }
    }


    @Override
    public Map supplementProductA(String factoryCode) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 100);
        resultMap.put("msg", "检疫证号:" + factoryCode + "未查询到！");
        if (StringUtils.isEmpty(factoryCode)) {
            resultMap.put("msg", "检疫证号不能为空！");
            return resultMap;
        }
        List<Jyzscpb> jyzscpbs = tccpaService.getLocalPCAAList(null, null, factoryCode);
        if (jyzscpbs != null && jyzscpbs.size() > 0) {
            Integer result = splitDataSummary.interceptListProduct(jyzscpbs, PRODUCT_A);
            if (result != null && result.intValue() > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "补充了ProductA:" + factoryCode);
                return resultMap;
            } else {
                resultMap.put("code", 100);
                resultMap.put("msg", "检疫证号：" + factoryCode + "查询到了，推送农业部失败了！");
                return resultMap;
            }
        } else {
            return resultMap;
        }
    }


    @Override
    public Map supplementProductB(String factoryCode) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 100);
        resultMap.put("msg", "检疫证号:" + factoryCode + "未查询到！");
        if (StringUtils.isEmpty(factoryCode)) {
            resultMap.put("msg", "检疫证号不能为空！");
            return resultMap;
        }
        List<Jyzscpb> jyzscpbs = tccpbService.getLocalCCPBList(null, null, factoryCode);
        if (jyzscpbs != null && jyzscpbs.size() > 0) {
            Integer result = splitDataSummary.interceptListProduct(jyzscpbs, PRODUCT_B);
            if (result != null && result.intValue() > 0) {
                resultMap.put("code", 200);
                resultMap.put("msg", "补充了ProductB:" + factoryCode);
                return resultMap;
            } else {
                resultMap.put("code", 100);
                resultMap.put("msg", "检疫证号：" + factoryCode + "查询到了，推送农业部失败了！");
                return resultMap;
            }
        } else {
            return resultMap;
        }
    }


    @Override
    public Map cancelCertificate(Integer type, String factoryCode) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 100);
        resultMap.put("msg", "检疫证号:" + factoryCode + "未查询到！");
        if (StringUtils.isEmpty(factoryCode) || type == null) {
            resultMap.put("msg", "检疫证号不能为空！");
            return resultMap;
        }
        JSONObject _Status = new JSONObject();
        _Status.put("key", Constant.RECEIVED_RECEIVED);
        _Status.put("Name", "作废");
        Integer result = 0;
        if (type.intValue() == 1) {
            TCCAAEntity tccaaEntity = tccaaService.selectTCCAAInfoByFactoryCode(factoryCode);
            if (tccaaEntity == null) return resultMap;
            result = tccaaService.updateTCCAAStatus(_Status.toJSONString(), DateUtil.dateToString(new Date()), tccaaEntity.getMid());
        } else if (type.intValue() == 2) {
            TCCABEntity tccabEntity = tccabService.selectTCCABInfoByFactoryCode(factoryCode);
            if (tccabEntity == null) return resultMap;
            result = tccabService.updateTCCABStatus(_Status.toJSONString(), DateUtil.dateToString(new Date()), tccabEntity.getMid());
        } else if (type.intValue() == 3) {
            TCCPAEntity tccpaEntity = tccpaService.selectProductAByFactoryCode(factoryCode);
            if (tccpaEntity == null) return resultMap;
            result = tccpaService.updateTCCPAInfo(_Status.toJSONString(), tccpaEntity.getMid(), _Status.getInteger("key"));
        } else if (type.intValue() == 4) {
            TCCPBEntity tccpbEntity = tccpbService.selectTCCPBInfoByFactoryCode(factoryCode);
            if (tccpbEntity == null) return resultMap;
            result = tccpbService.updateTCCPBStatus(_Status.toJSONString(), DateUtil.dateToString(new Date()), tccpbEntity.getMid());
        }

        if (result != null && result.intValue() > 0) {
            resultMap.put("code", 200);
            resultMap.put("msg", "作废了检疫证:" + factoryCode);
        }
        return resultMap;
    }
}
